<!DOCTYPE html>


<html lang="en">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    JavaScript实现文本编辑器 【table相关操作】 |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
      <canvas width="1777" height="841"
        style="position: fixed; left: 0px; top: 0px; z-index: 99999; pointer-events: none;"></canvas>
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-table"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  JavaScript实现文本编辑器 【table相关操作】
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/02/27/table/" class="article-date">
  <time datetime="2020-02-27T02:16:00.000Z" itemprop="datePublished">2020-02-27</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/JavaScript/">JavaScript</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">5.1k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">27 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <pre>
  我自己写的基本操作可以实现
  就是table结构改变了之后 在操作会出现一些问题
  目前没想通要怎么处理 
  查了下看到一个大佬用jQuery写的合并拆分
  基本把我的一些bug解决了 但是我没有看懂他的逻辑QAQ
</pre>
<h2 id="页面结构"><a href="#页面结构" class="headerlink" title="页面结构"></a>页面结构</h2><blockquote>
<p>table的底部和侧边有一个  +  号标记可以添加行/列</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">onclick</span>=<span class="string">&quot;delTable()&quot;</span>&gt;</span>delete<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">onclick</span>=<span class="string">&quot;addCell()&quot;</span>&gt;</span>addCell<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">onclick</span>=<span class="string">&quot;addRow()&quot;</span>&gt;</span>addRow<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">onclick</span>=<span class="string">&quot;mergeTable()&quot;</span>&gt;</span>合并单元格<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">onclick</span>=<span class="string">&quot;mergeDown()&quot;</span>&gt;</span>向下合并单元格<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">onclick</span>=<span class="string">&quot;mergeRight()&quot;</span>&gt;</span>向右合并单元格<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">button</span> <span class="attr">onclick</span>=<span class="string">&quot;splitTable()&quot;</span>&gt;</span>拆分单元格<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;table&quot;</span>  <span class="attr">contentEditable</span>=<span class="string">&#x27;true&#x27;</span> &gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">table</span> <span class="attr">border</span>=<span class="string">&quot;1&quot;</span> <span class="attr">id</span>=<span class="string">&quot;table&quot;</span>&gt;</span></span><br><span class="line">				...</span><br><span class="line">			<span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;addCell&quot;</span> <span class="attr">onclick</span>=<span class="string">&quot;addCell()&quot;</span>&gt;</span>+<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&#x27;addRow&#x27;</span> <span class="attr">onclick</span>=<span class="string">&quot;addRow()&quot;</span>&gt;</span>+<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="删除功能"><a href="#删除功能" class="headerlink" title="删除功能"></a>删除功能</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">  delTable: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="built_in">this</span>.table.remove() <span class="comment">//dom自带方法</span></span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h2 id="添加列功能"><a href="#添加列功能" class="headerlink" title="添加列功能"></a>添加列功能</h2><h3 id="trObject-insertCell-index"><a href="#trObject-insertCell-index" class="headerlink" title="trObject.insertCell( index )"></a><a target="_blank" rel="noopener" href="https://www.runoob.com/jsref/met-tablerow-insertcell.html">trObject.insertCell( index )</a></h3><p>insertCell() 方法用于在 HTML 表的一行的指定位置插入一个空的 &lt;td&gt; 元素。<br>新单元格将被插入当前位于 index 指定位置的表元之前。如果 index 等于行中的单元格数，则新单元格被附加在行的末尾。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   *  insertCell( index ) table的原生方法,参数传的是添加的位置</span></span><br><span class="line"><span class="comment">   * 需要注意的是添加之后要重新给列表宽度赋值</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">  addCell: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; table.rows.length; i++) &#123;</span><br><span class="line">		<span class="keyword">var</span> newCell = table.rows[i].insertCell(<span class="built_in">this</span>.table.colspan)</span><br><span class="line">		newCell.innerHTML = <span class="string">&#x27;new add&#x27;</span></span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">let</span> j = <span class="number">0</span>; j &lt; table.rows[i].cells.length; j++) &#123;</span><br><span class="line">			<span class="built_in">this</span>.table.rows[i].cells[j].style.width = <span class="number">900</span> / <span class="built_in">this</span>.table.rows[<span class="number">0</span>].cells.length + <span class="number">1</span> + <span class="string">&#x27;px&#x27;</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h2 id="添加行功能"><a href="#添加行功能" class="headerlink" title="添加行功能"></a>添加行功能</h2><h3 id="tableObject-insertRow-index"><a href="#tableObject-insertRow-index" class="headerlink" title="tableObject.insertRow( index )"></a><a target="_blank" rel="noopener" href="https://www.runoob.com/jsref/met-table-insertrow.html">tableObject.insertRow( index )</a></h3><p>insertRow() 方法用于在表格中的指定位置插入一个新行。<br>index : 指定插入新行的位置 (以 0 开始)。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 被注释的那一段的想法是 在哪点击了就以点击的那一行为模板 在那行下面添加该行内容</span></span><br><span class="line"><span class="comment">   * 但是右侧的添加行功能 不能实现这一操作 就直接获取的第一行的内容来添加 </span></span><br><span class="line"><span class="comment">   * 应该是有bug的</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">  addRow: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> newRow = <span class="built_in">this</span>.table.insertRow(<span class="built_in">this</span>.table.rowspan)</span><br><span class="line">	newRow.innerHTML = <span class="built_in">this</span>.table.rows[<span class="number">0</span>].innerHTML <span class="comment">//document.getSelection().getRangeAt(0).startContainer.parentNode.innerHTML</span></span><br><span class="line">&#125;, </span><br></pre></td></tr></table></figure>
<h2 id="合并功能"><a href="#合并功能" class="headerlink" title="合并功能"></a>合并功能</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 这个是我写的 直接获取选中的td 然后替换删除</span></span><br><span class="line"><span class="comment">   * 没有考虑到已经合并的情况 是有问题的</span></span><br><span class="line"><span class="comment"> * 我的想法是获取带有选中类的节点，创建一个新的节点 去替换</span></span><br><span class="line"><span class="comment"> * 其余的选中的节点自己删除</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">  mergeTable: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> nodes = <span class="built_in">this</span>.table.getElementsByClassName(<span class="string">&#x27;ui-selected&#x27;</span>)</span><br><span class="line">	<span class="keyword">var</span> newNode = <span class="built_in">document</span>.createElement(<span class="string">&#x27;td&#x27;</span>)</span><br><span class="line">	<span class="keyword">var</span> parent = nodes[<span class="number">0</span>].parentNode</span><br><span class="line">	newNode.rowSpan = <span class="built_in">this</span>.table.rowspan</span><br><span class="line">	newNode.colSpan = <span class="built_in">this</span>.table.colspan</span><br><span class="line">	parent.replaceChild(newNode, nodes[<span class="number">0</span>])</span><br><span class="line">	<span class="keyword">while</span> (nodes[<span class="number">0</span>]) &#123;</span><br><span class="line">		nodes[<span class="number">0</span>].remove()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 这个是CSDN某大佬借助jquery写的 我没看懂QAQ</span></span><br><span class="line"><span class="comment">   * 原文地址 2楼： https://bbs.csdn.net/topics/391807641 </span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">  mergeTable: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> $t = $(<span class="string">&quot;#&quot;</span> + <span class="built_in">this</span>.tableId);</span><br><span class="line">	<span class="keyword">if</span> ($(<span class="string">&quot;table&quot;</span>, $t).length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">		alert(<span class="string">&quot;不支持嵌套表格！&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">var</span> sigDel = <span class="string">&quot;sign4delete&quot;</span>; <span class="comment">// 删除标记，用作类名</span></span><br><span class="line">	<span class="keyword">var</span> sigSel = <span class="string">&quot;ui-selected&quot;</span>; <span class="comment">// 选中标记，用作类名</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">// 补充单元格以便后继正确计算坐标</span></span><br><span class="line">	$(<span class="string">&quot;th,td&quot;</span>, $t).each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">		<span class="comment">// 坐标要实时计算，因会实时补充</span></span><br><span class="line">		<span class="keyword">var</span> ridx = $(<span class="string">&quot;tr&quot;</span>, $t).index($(<span class="built_in">this</span>).parent(<span class="string">&quot;tr&quot;</span>));</span><br><span class="line">		<span class="keyword">var</span> cidx = $(<span class="built_in">this</span>).parent().children(<span class="string">&quot;th,td&quot;</span>).index(<span class="built_in">this</span>);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">var</span> rowspan = <span class="built_in">Number</span>($(<span class="built_in">this</span>).attr(<span class="string">&quot;rowspan&quot;</span>)) || <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">var</span> colspan = <span class="built_in">Number</span>($(<span class="built_in">this</span>).attr(<span class="string">&quot;colspan&quot;</span>)) || <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">var</span> isSel = $(<span class="built_in">this</span>).hasClass(sigSel);</span><br><span class="line">		<span class="comment">// 非选单元格拆出的单元格要加删除标记</span></span><br><span class="line">		<span class="keyword">if</span> (rowspan &lt;= <span class="number">1</span> &amp;&amp; colspan &lt;= <span class="number">1</span>)</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		<span class="comment">// 跨格开插</span></span><br><span class="line">		$(<span class="string">&quot;tr&quot;</span>, $t).each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">var</span> idx = $(<span class="string">&quot;tr&quot;</span>, $t).index(<span class="built_in">this</span>);</span><br><span class="line">			<span class="keyword">var</span> arr, $td = $(<span class="string">&quot;&lt;td&gt;&quot;</span>).addClass(isSel ? sigSel : sigDel);</span><br><span class="line"></span><br><span class="line">			<span class="keyword">if</span> (idx == ridx) &#123;</span><br><span class="line">				<span class="comment">// 本行在 [cidx] 后插入 colspan-1 个</span></span><br><span class="line"></span><br><span class="line">				arr = $(); <span class="comment">// 准备待插单元格</span></span><br><span class="line">				<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; colspan - <span class="number">1</span>; i++)</span><br><span class="line">					arr = arr.add($td.clone());</span><br><span class="line">				<span class="comment">// 插入</span></span><br><span class="line">				$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).after(arr);</span><br><span class="line"></span><br><span class="line">			&#125; <span class="keyword">else</span> <span class="keyword">if</span> (ridx &lt; idx &amp;&amp; idx &lt; ridx + rowspan) &#123;</span><br><span class="line">				<span class="comment">// 以下行在 [cidx] 前插入 colspan 个</span></span><br><span class="line"></span><br><span class="line">				arr = $(); <span class="comment">// 准备待插单元格</span></span><br><span class="line">				<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; colspan; i++)</span><br><span class="line">					arr = arr.add($td.clone());</span><br><span class="line">				<span class="comment">// 插入</span></span><br><span class="line">				<span class="keyword">if</span> (cidx &gt; <span class="number">0</span> &amp;&amp; $(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx - <span class="number">1</span>).length &gt; <span class="number">0</span>)</span><br><span class="line">					$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx - <span class="number">1</span>).after(arr);</span><br><span class="line">				<span class="keyword">else</span> <span class="keyword">if</span> ($(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).length &gt; <span class="number">0</span>)</span><br><span class="line">					$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).before(arr);</span><br><span class="line">				<span class="keyword">else</span></span><br><span class="line">					$(<span class="built_in">this</span>).prepend(arr);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">var</span> rmin = <span class="number">10000</span>,</span><br><span class="line">		cmin = <span class="number">10000</span>;</span><br><span class="line">	<span class="keyword">var</span> rmax = <span class="number">0</span>,</span><br><span class="line">		cmax = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">var</span> rnum, cnum;</span><br><span class="line">	<span class="comment">// 计算起始和跨距</span></span><br><span class="line">	$(<span class="string">&quot;th,td&quot;</span>, $t).filter(<span class="string">&quot;.&quot;</span> + sigSel).each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">		<span class="keyword">var</span> ridx = $(<span class="string">&quot;tr&quot;</span>, $t).index($(<span class="built_in">this</span>).parent(<span class="string">&quot;tr&quot;</span>));</span><br><span class="line">		rmin = ridx &lt; rmin ? ridx : rmin;</span><br><span class="line">		rmax = ridx &gt; rmax ? ridx : rmax;</span><br><span class="line">		<span class="keyword">var</span> cidx = $(<span class="built_in">this</span>).parent().children(<span class="string">&quot;th,td&quot;</span>).index(<span class="built_in">this</span>);</span><br><span class="line">		cmin = cidx &lt; cmin ? cidx : cmin;</span><br><span class="line">		cmax = cidx &gt; cmax ? cidx : cmax;</span><br><span class="line">	&#125;);</span><br><span class="line">	rnum = rmax - rmin + <span class="number">1</span>;</span><br><span class="line">	cnum = cmax - cmin + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 合并单元格</span></span><br><span class="line">	$(<span class="string">&quot;th,td&quot;</span>, $t).each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">		<span class="keyword">var</span> ridx = $(<span class="string">&quot;tr&quot;</span>, $t).index($(<span class="built_in">this</span>).parent(<span class="string">&quot;tr&quot;</span>));</span><br><span class="line">		<span class="keyword">var</span> cidx = $(<span class="built_in">this</span>).parent().children(<span class="string">&quot;th,td&quot;</span>).index(<span class="built_in">this</span>);</span><br><span class="line">		<span class="comment">// 标记单元格待删</span></span><br><span class="line">		<span class="keyword">if</span> (rmin &lt;= ridx &amp;&amp; ridx &lt;= rmax &amp;&amp;</span><br><span class="line">			cmin &lt;= cidx &amp;&amp; cidx &lt;= cmax)</span><br><span class="line">			$(<span class="built_in">this</span>).addClass(sigDel);</span><br><span class="line">		<span class="comment">// 处理被选左上角单元格</span></span><br><span class="line">		<span class="keyword">if</span> (ridx == rmin &amp;&amp; cidx == cmin)</span><br><span class="line">			$(<span class="built_in">this</span>).removeClass(sigDel).attr(&#123;</span><br><span class="line">				rowspan: rnum,</span><br><span class="line">				colspan: cnum</span><br><span class="line">			&#125;);</span><br><span class="line">		<span class="comment">// 清理残余</span></span><br><span class="line">		<span class="keyword">if</span> ($(<span class="built_in">this</span>).attr(<span class="string">&quot;rowspan&quot;</span>) == <span class="number">1</span>) $(<span class="built_in">this</span>).removeAttr(<span class="string">&quot;rowspan&quot;</span>);</span><br><span class="line">		<span class="keyword">if</span> ($(<span class="built_in">this</span>).attr(<span class="string">&quot;colspan&quot;</span>) == <span class="number">1</span>) $(<span class="built_in">this</span>).removeAttr(<span class="string">&quot;colspan&quot;</span>);</span><br><span class="line">	&#125;).remove(<span class="string">&quot;.&quot;</span> + sigDel);</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h2 id="向下合并功能"><a href="#向下合并功能" class="headerlink" title="向下合并功能"></a>向下合并功能</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">  mergeDown: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> row = <span class="built_in">document</span>.getSelection().anchorNode.parentNode.rowIndex</span><br><span class="line">	<span class="keyword">var</span> cell = <span class="built_in">document</span>.getSelection().anchorNode.cellIndex</span><br><span class="line">	<span class="keyword">var</span> node = <span class="built_in">document</span>.getSelection().anchorNode</span><br><span class="line">	<span class="keyword">if</span> (row != <span class="built_in">this</span>.table.rows.length - <span class="number">1</span> &amp;&amp; node.rowSpan == <span class="number">1</span>) &#123;</span><br><span class="line">		node.rowSpan = <span class="number">2</span></span><br><span class="line">		<span class="built_in">this</span>.table.rows[row + <span class="number">1</span>].cells[cell].remove()</span><br><span class="line">	&#125;</span><br><span class="line">&#125;,    </span><br></pre></td></tr></table></figure>
<h2 id="向右合并功能"><a href="#向右合并功能" class="headerlink" title="向右合并功能"></a>向右合并功能</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">  mergeRight: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> cell = <span class="built_in">document</span>.getSelection().anchorNode.cellIndex</span><br><span class="line">	<span class="keyword">var</span> node = <span class="built_in">document</span>.getSelection().anchorNode</span><br><span class="line">	<span class="keyword">if</span> (cell != <span class="number">0</span> &amp;&amp; node.colSpan == <span class="number">1</span>) &#123;</span><br><span class="line">		node.previousElementSibling.remove()</span><br><span class="line">		node.colSpan = <span class="number">2</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h2 id="拆分功能"><a href="#拆分功能" class="headerlink" title="拆分功能"></a>拆分功能</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 我实现的 因为在ueditor里面是只可以拆分两格的</span></span><br><span class="line"><span class="comment">   * 我就按照它的写的 只能拆分两格的</span></span><br><span class="line"><span class="comment">   * 但是多格拆分应该也差不多</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"> splitTable: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> node = <span class="built_in">document</span>.getSelection().anchorNode</span><br><span class="line">	<span class="keyword">var</span> td = <span class="built_in">document</span>.createElement(<span class="string">&#x27;td&#x27;</span>)</span><br><span class="line">	<span class="keyword">if</span> (node.rowSpan == <span class="number">2</span> &amp;&amp; node.colSpan == <span class="number">1</span>) &#123;</span><br><span class="line">		node.rowSpan = <span class="number">1</span></span><br><span class="line">		node.parentNode.nextElementSibling.appendChild(td)</span><br><span class="line">	&#125; <span class="keyword">else</span> <span class="keyword">if</span> (node.colSpan == <span class="number">2</span> &amp;&amp; node.rowSpan == <span class="number">1</span>) &#123;</span><br><span class="line">		node.colSpan = <span class="number">1</span></span><br><span class="line">		node.parentNode.appendChild(td)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 也是那个大佬的代码 可以拆分多个单元格</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">  splitTable: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> $t = $(<span class="string">&quot;#&quot;</span>+<span class="built_in">this</span>.tableId);</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span> ($(<span class="string">&quot;table&quot;</span>, $t).length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">			alert(<span class="string">&quot;不支持嵌套表格！&quot;</span>);</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">var</span> sigDel = <span class="string">&quot;sign4delete&quot;</span>;  <span class="comment">// 删除标记，类名，自定义</span></span><br><span class="line">		<span class="keyword">var</span> sigSel = <span class="string">&quot;ui-selected&quot;</span>;  <span class="comment">// 选中标记，类名，jQuery UI 定义</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">// 补充单元格以便后继正确计算坐标</span></span><br><span class="line">		$(<span class="string">&quot;th,td&quot;</span>, $t).each(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="comment">// 坐标要实时计算，因会实时补充</span></span><br><span class="line">			<span class="keyword">var</span> ridx = $(<span class="string">&quot;tr&quot;</span>, $t).index($(<span class="built_in">this</span>).parent(<span class="string">&quot;tr&quot;</span>));</span><br><span class="line">			<span class="keyword">var</span> cidx = $(<span class="built_in">this</span>).parent().children(<span class="string">&quot;th,td&quot;</span>).index(<span class="built_in">this</span>);</span><br><span class="line">			<span class="keyword">var</span> rowspan = <span class="built_in">Number</span>($(<span class="built_in">this</span>).attr(<span class="string">&quot;rowspan&quot;</span>)) || <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">var</span> colspan = <span class="built_in">Number</span>($(<span class="built_in">this</span>).attr(<span class="string">&quot;colspan&quot;</span>)) || <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">var</span> isSel = $(<span class="built_in">this</span>).hasClass(sigSel);</span><br><span class="line">			<span class="comment">// 非选单元格拆出的单元格要加删除标记</span></span><br><span class="line">			</span><br><span class="line">			<span class="keyword">if</span> (rowspan &lt;= <span class="number">1</span> &amp;&amp; colspan &lt;= <span class="number">1</span>)</span><br><span class="line">				<span class="keyword">return</span>;</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">if</span> (isSel)</span><br><span class="line">				$(<span class="built_in">this</span>).removeAttr(<span class="string">&quot;colspan&quot;</span>).removeAttr(<span class="string">&quot;rowspan&quot;</span>);</span><br><span class="line">			</span><br><span class="line">			<span class="comment">// 跨格开插</span></span><br><span class="line">			$(<span class="string">&quot;tr&quot;</span>, $t).each(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">				<span class="keyword">var</span> idx = $(<span class="string">&quot;tr&quot;</span>, $t).index(<span class="built_in">this</span>);</span><br><span class="line">				<span class="keyword">var</span> arr, $td = $(<span class="string">&quot;&lt;td&gt;&quot;</span>);</span><br><span class="line">				</span><br><span class="line">				<span class="keyword">if</span> (!isSel)</span><br><span class="line">					$td.addClass(sigDel);</span><br><span class="line">				</span><br><span class="line">				<span class="keyword">if</span> (idx == ridx) &#123;</span><br><span class="line">					<span class="comment">// 本行在 [cidx] 后插入 colspan-1 个</span></span><br><span class="line">					</span><br><span class="line">					arr = $();  <span class="comment">// 准备待插单元格</span></span><br><span class="line">					<span class="keyword">for</span> (<span class="keyword">var</span> i=<span class="number">0</span>; i &lt; colspan<span class="number">-1</span>; i++)</span><br><span class="line">						arr = arr.add($td.clone());</span><br><span class="line">					</span><br><span class="line">					$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).after(arr);</span><br><span class="line">					</span><br><span class="line">				&#125; <span class="keyword">else</span> <span class="keyword">if</span> (ridx &lt; idx &amp;&amp; idx &lt; ridx + rowspan) &#123;</span><br><span class="line">					<span class="comment">// 以下行在 [cidx] 前插入 colspan 个</span></span><br><span class="line">					</span><br><span class="line">					arr = $();  <span class="comment">// 准备待插单元格</span></span><br><span class="line">					<span class="keyword">for</span> (<span class="keyword">var</span> i=<span class="number">0</span>; i &lt; colspan; i++)</span><br><span class="line">						arr = arr.add($td.clone());</span><br><span class="line">					</span><br><span class="line">					<span class="keyword">if</span> (cidx &gt; <span class="number">0</span> &amp;&amp; $(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx - <span class="number">1</span>).length &gt; <span class="number">0</span>)</span><br><span class="line">						$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx - <span class="number">1</span>).after(arr);</span><br><span class="line">					<span class="keyword">else</span> <span class="keyword">if</span> ($(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).length &gt; <span class="number">0</span>)</span><br><span class="line">						$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).before(arr);</span><br><span class="line">					<span class="keyword">else</span></span><br><span class="line">						$(<span class="built_in">this</span>).prepend(arr);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;);</span><br><span class="line">		&#125;);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">// 重新获取以取到删者并删之</span></span><br><span class="line">		$(<span class="string">&quot;th,td&quot;</span>, $t).remove(<span class="string">&quot;.&quot;</span> + sigDel);</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h2 id="鼠标长按选中事件"><a href="#鼠标长按选中事件" class="headerlink" title="鼠标长按选中事件"></a>鼠标长按选中事件</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line">   <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 点击选取范围那还是有问题的不知道怎么解决</span></span><br><span class="line"><span class="comment">    * 主要是针对已经合并的单元格 怎么遍历的问题</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">   <span class="built_in">this</span>.table.onmousedown = <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">	<span class="comment">// 鼠标按下的时候获取所在单元格的坐标，和鼠标按下的时间</span></span><br><span class="line">	<span class="built_in">this</span>.startRow = e.target.parentNode.rowIndex</span><br><span class="line">	<span class="built_in">this</span>.startCell = e.target.cellIndex</span><br><span class="line">	<span class="built_in">this</span>.startDate = <span class="keyword">new</span> <span class="built_in">Date</span>()</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="built_in">this</span>.table.onmouseup = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="comment">// 通过和鼠标抬起事件所获取的时间进行对比 判断是否是长按鼠标事件</span></span><br><span class="line">	<span class="keyword">var</span> isClick = <span class="keyword">new</span> <span class="built_in">Date</span>() - <span class="built_in">this</span>.startDate &lt; <span class="number">500</span> ? <span class="literal">true</span> : <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 获取鼠标抬起时 鼠标所在单元格的坐标点</span></span><br><span class="line">	<span class="keyword">var</span> endCell = event.target.cellIndex</span><br><span class="line">	<span class="keyword">var</span> endRow = event.target.parentNode.rowIndex</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (!isClick) &#123;</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="built_in">Math</span>.min(endRow, <span class="built_in">this</span>.startRow); i &lt;= <span class="built_in">Math</span>.max(endRow, <span class="built_in">this</span>.startRow); i++) &#123;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">let</span> j = <span class="built_in">Math</span>.min(endCell, <span class="built_in">this</span>.startCell); j &lt;= <span class="built_in">Math</span>.max(endCell, <span class="built_in">this</span>.startCell); j++) &#123;</span><br><span class="line">				<span class="comment">// 以获取对角坐标 填充选中的区域  只能针对结构未改变的table</span></span><br><span class="line">				<span class="comment">//this.rows[i].cells[j].className = &#x27;ui-selected&#x27;</span></span><br><span class="line"></span><br><span class="line">				<span class="keyword">if</span> (<span class="built_in">this</span>.rows[i].cells[j]) &#123;</span><br><span class="line">					<span class="keyword">if</span> (<span class="built_in">this</span>.rows[i].cells[j].rowSpan &gt; <span class="number">1</span>) &#123;</span><br><span class="line">						<span class="keyword">if</span> (j == <span class="built_in">Math</span>.max(endCell, <span class="built_in">this</span>.startCell)) &#123;</span><br><span class="line">							<span class="keyword">for</span> (<span class="keyword">let</span> a = <span class="number">1</span>; a &lt; <span class="built_in">this</span>.rows[i].cells[j].rowSpan; a++) &#123;</span><br><span class="line">								<span class="keyword">if</span>(<span class="built_in">this</span>.rows[i + a].cells[j])&#123;</span><br><span class="line">									<span class="built_in">this</span>.rows[i + a].cells[j].className = <span class="string">&#x27;del&#x27;</span></span><br><span class="line">								&#125;</span><br><span class="line">							&#125;</span><br><span class="line">						&#125;<span class="keyword">else</span> <span class="keyword">if</span>((j + table.rows[i].cells[j].colSpan - <span class="number">1</span> )== <span class="built_in">Math</span>.max(endCell, <span class="built_in">this</span>.startCell))&#123;</span><br><span class="line">							<span class="keyword">for</span> (<span class="keyword">let</span> a = <span class="number">1</span>; a &lt; <span class="built_in">this</span>.rows[i].cells[j].rowSpan; a++) &#123;</span><br><span class="line">								<span class="keyword">for</span> (<span class="keyword">let</span> b = <span class="number">0</span>; b &lt; <span class="built_in">this</span>.rows[i].cells[j].colSpan; b++) &#123;</span><br><span class="line">									<span class="keyword">if</span> (<span class="built_in">this</span>.rows[i + a].cells[j+b]) &#123;</span><br><span class="line">										<span class="built_in">this</span>.rows[i + a].cells[j+b].className = <span class="string">&#x27;del&#x27;</span></span><br><span class="line">									&#125;</span><br><span class="line">								&#125;</span><br><span class="line">							&#125;</span><br><span class="line">						&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">							 <span class="keyword">var</span> maxJ = <span class="built_in">Math</span>.max(endCell, <span class="built_in">this</span>.startCell)</span><br><span class="line">							<span class="keyword">for</span> (<span class="keyword">let</span> a = <span class="number">0</span>; a &lt; <span class="built_in">this</span>.rows[i].cells[j].rowSpan; a++) &#123;</span><br><span class="line">								<span class="keyword">if</span> (table.rows[i + a].cells[maxJ]) &#123;</span><br><span class="line">									<span class="built_in">this</span>.rows[i + a].cells[maxJ].className = <span class="string">&#x27;del&#x27;</span></span><br><span class="line">								&#125;</span><br><span class="line">							&#125; </span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line"></span><br><span class="line">					<span class="keyword">if</span> (<span class="built_in">this</span>.rows[i].cells[j].colSpan &gt; <span class="number">1</span>) &#123;</span><br><span class="line">						<span class="keyword">if</span> (j !== endCell &amp;&amp; j !== <span class="built_in">this</span>.startCell) &#123;</span><br><span class="line">							<span class="keyword">for</span> (<span class="keyword">let</span> b = <span class="number">1</span>; b &lt; <span class="built_in">this</span>.rows[i].cells[j].colSpan; b++) &#123;</span><br><span class="line">								<span class="keyword">if</span>(<span class="built_in">this</span>.rows[i].cells[j + b])&#123;</span><br><span class="line">									<span class="built_in">this</span>.rows[i].cells[j + b].className = <span class="string">&#x27;del&#x27;</span></span><br><span class="line">								&#125;			</span><br><span class="line">							&#125;</span><br><span class="line">						&#125;<span class="keyword">else</span> <span class="keyword">if</span>((j + table.rows[i].cells[j].colSpan - <span class="number">1</span> )== <span class="built_in">Math</span>.max(endCell, <span class="built_in">this</span>.startCell))&#123;</span><br><span class="line">							<span class="keyword">for</span> (<span class="keyword">let</span> a = <span class="number">0</span>; a &lt; <span class="built_in">this</span>.rows[i].cells[j].rowSpan; a++) &#123;</span><br><span class="line">								<span class="keyword">for</span> (<span class="keyword">let</span> b = <span class="number">1</span>; b &lt; <span class="built_in">this</span>.rows[i].cells[j].colSpan; b++) &#123;</span><br><span class="line">									<span class="keyword">if</span> (<span class="built_in">this</span>.rows[i + a].cells[j+b]) &#123;</span><br><span class="line">										<span class="built_in">this</span>.rows[i + a].cells[j+b].className = <span class="string">&#x27;del&#x27;</span></span><br><span class="line">									&#125;</span><br><span class="line">								&#125;</span><br><span class="line">							&#125;	</span><br><span class="line">						&#125; </span><br><span class="line">						<span class="keyword">else</span> &#123;</span><br><span class="line">							<span class="built_in">this</span>.rows[i].cells[j].className = <span class="string">&#x27;&#x27;</span></span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="built_in">this</span>.rows[i].cells[j].className += <span class="string">&#x27;ui-selected&#x27;</span></span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// 在table中增加两个属性 获取到跨的行和列 供合并单元格</span></span><br><span class="line">		<span class="built_in">this</span>.rowspan = <span class="built_in">Math</span>.abs(endRow - <span class="built_in">this</span>.startRow) + <span class="number">1</span></span><br><span class="line">		<span class="built_in">this</span>.colspan = <span class="built_in">Math</span>.abs(endCell - <span class="built_in">this</span>.startCell) + <span class="number">1</span></span><br><span class="line"></span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="comment">// 清空样式</span></span><br><span class="line">		<span class="built_in">this</span>.rowspan = endRow</span><br><span class="line">		<span class="built_in">this</span>.colspan = endCell</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; table.rows.length; i++) &#123;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">let</span> j = <span class="number">0</span>; j &lt; table.rows[i].cells.length; j++) &#123;</span><br><span class="line">				<span class="built_in">this</span>.rows[i].cells[j].className = <span class="string">&#x27;&#x27;</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="鼠标进入离开显示添加行列"><a href="#鼠标进入离开显示添加行列" class="headerlink" title="鼠标进入离开显示添加行列"></a>鼠标进入离开显示添加行列</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">   <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * offset值好像是按照鼠标离每个td的位置不是离tr的位置</span></span><br><span class="line"><span class="comment">    * 所以需要利用client来处理下距离的判断</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">   <span class="built_in">this</span>.table.onmouseleave = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">		addCellBtn.style.display = <span class="string">&#x27;none&#x27;</span></span><br><span class="line">		addRowBtn.style.display = <span class="string">&#x27;none&#x27;</span></span><br><span class="line">	&#125;, <span class="number">3000</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">this</span>.table.onmousemove = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (event.clientX - <span class="built_in">this</span>.offsetLeft &gt;= <span class="number">8</span> &amp;&amp; event.clientX - <span class="built_in">this</span>.offsetLeft &lt;= <span class="number">20</span>) &#123;</span><br><span class="line">		<span class="built_in">this</span>.style.cursor = <span class="string">&quot;url(&#x27;img/right.png&#x27;) , auto&quot;</span></span><br><span class="line">		<span class="built_in">this</span>.colspan = <span class="built_in">this</span>.rows.length</span><br><span class="line">	&#125; <span class="keyword">else</span> <span class="keyword">if</span> (event.clientY - <span class="built_in">this</span>.offsetTop &gt;= <span class="number">52</span> &amp;&amp; event.clientY - <span class="built_in">this</span>.offsetTop &lt;= <span class="number">60</span>) &#123;</span><br><span class="line">		<span class="built_in">this</span>.style.cursor = <span class="string">&quot;url(&#x27;img/down.png&#x27;) , auto&quot;</span></span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="built_in">this</span>.style.cursor = <span class="string">&#x27;auto&#x27;</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="鼠标点击选中一行一列事件"><a href="#鼠标点击选中一行一列事件" class="headerlink" title="鼠标点击选中一行一列事件"></a>鼠标点击选中一行一列事件</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 判断鼠标的指针状态然后执行点击事件 选中一行/列</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">   <span class="built_in">this</span>.table.onclick = <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">		<span class="keyword">if</span> (<span class="built_in">this</span>.style.cursor !== <span class="string">&#x27;auto&#x27;</span>) &#123;</span><br><span class="line">			<span class="keyword">if</span> (<span class="built_in">this</span>.style.cursor.includes(<span class="string">&#x27;down&#x27;</span>)) &#123;</span><br><span class="line">				<span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="built_in">this</span>.rows.length; i++) &#123;</span><br><span class="line">					<span class="built_in">this</span>.rows[i].cells[e.target.cellIndex].className = <span class="string">&#x27;ui-selected&#x27;</span></span><br><span class="line">				&#125;</span><br><span class="line">				<span class="built_in">this</span>.rowspan = <span class="built_in">this</span>.rows.length</span><br><span class="line">			&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">				<span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="built_in">this</span>.rows[e.target.parentNode.rowIndex].cells.length; i++) &#123;</span><br><span class="line">					<span class="built_in">this</span>.rows[e.target.parentNode.rowIndex].cells[i].className = <span class="string">&#x27;ui-selected&#x27;</span></span><br><span class="line">				&#125;</span><br><span class="line">				<span class="built_in">this</span>.colspan = <span class="built_in">this</span>.rows[e.target.parentNode.rowIndex].cells.length</span><br><span class="line">				<span class="built_in">this</span>.rowspan = <span class="number">1</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="JavaScript封装成类的完整代码"><a href="#JavaScript封装成类的完整代码" class="headerlink" title="JavaScript封装成类的完整代码"></a>JavaScript封装成类的完整代码</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params">win</span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> win_ = win;</span><br><span class="line">	<span class="keyword">var</span> kclass = <span class="function"><span class="keyword">function</span> (<span class="params">tableId</span>) </span>&#123;</span><br><span class="line">		<span class="built_in">this</span>._init.call(<span class="built_in">this</span>, <span class="built_in">arguments</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	kclass.fn = &#123;</span><br><span class="line"></span><br><span class="line">		tableId: <span class="string">&quot;table&quot;</span>,</span><br><span class="line">		table: <span class="literal">null</span>,</span><br><span class="line">		delTable: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="built_in">this</span>.table.remove()</span><br><span class="line">		&#125;,</span><br><span class="line">		addCell: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; table.rows.length; i++) &#123;</span><br><span class="line">				<span class="keyword">var</span> newCell = table.rows[i].insertCell(<span class="built_in">this</span>.table.colspan)</span><br><span class="line">				newCell.innerHTML = <span class="string">&#x27;new add&#x27;</span></span><br><span class="line">				<span class="keyword">for</span> (<span class="keyword">let</span> j = <span class="number">0</span>; j &lt; table.rows[i].cells.length; j++) &#123;</span><br><span class="line">					<span class="built_in">this</span>.table.rows[i].cells[j].style.width = <span class="number">900</span> / <span class="built_in">this</span>.table.rows[<span class="number">0</span>].cells.length + <span class="number">1</span> + <span class="string">&#x27;px&#x27;</span></span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;,</span><br><span class="line">		addRow: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">var</span> newRow = <span class="built_in">this</span>.table.insertRow(<span class="built_in">this</span>.table.rowspan)</span><br><span class="line">			newRow.innerHTML = <span class="built_in">this</span>.table.rows[<span class="number">0</span>].innerHTML <span class="comment">//document.getSelection().getRangeAt(0).startContainer.parentNode.innerHTML</span></span><br><span class="line">		&#125;,</span><br><span class="line">		mergeTable: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">var</span> $t = $(<span class="string">&quot;#&quot;</span> + <span class="built_in">this</span>.tableId);</span><br><span class="line">			<span class="keyword">if</span> ($(<span class="string">&quot;table&quot;</span>, $t).length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">				alert(<span class="string">&quot;不支持嵌套表格！&quot;</span>);</span><br><span class="line">				<span class="keyword">return</span>;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			<span class="keyword">var</span> sigDel = <span class="string">&quot;sign4delete&quot;</span>; <span class="comment">// 删除标记，用作类名</span></span><br><span class="line">			<span class="keyword">var</span> sigSel = <span class="string">&quot;ui-selected&quot;</span>; <span class="comment">// 选中标记，用作类名</span></span><br><span class="line"></span><br><span class="line">			<span class="comment">// 补充单元格以便后继正确计算坐标</span></span><br><span class="line">			$(<span class="string">&quot;th,td&quot;</span>, $t).each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">				<span class="comment">// 坐标要实时计算，因会实时补充</span></span><br><span class="line">				<span class="keyword">var</span> ridx = $(<span class="string">&quot;tr&quot;</span>, $t).index($(<span class="built_in">this</span>).parent(<span class="string">&quot;tr&quot;</span>));</span><br><span class="line">				<span class="keyword">var</span> cidx = $(<span class="built_in">this</span>).parent().children(<span class="string">&quot;th,td&quot;</span>).index(<span class="built_in">this</span>);</span><br><span class="line"></span><br><span class="line">				<span class="keyword">var</span> rowspan = <span class="built_in">Number</span>($(<span class="built_in">this</span>).attr(<span class="string">&quot;rowspan&quot;</span>)) || <span class="number">1</span>;</span><br><span class="line">				<span class="keyword">var</span> colspan = <span class="built_in">Number</span>($(<span class="built_in">this</span>).attr(<span class="string">&quot;colspan&quot;</span>)) || <span class="number">1</span>;</span><br><span class="line">				<span class="keyword">var</span> isSel = $(<span class="built_in">this</span>).hasClass(sigSel);</span><br><span class="line">				<span class="comment">// 非选单元格拆出的单元格要加删除标记</span></span><br><span class="line">				<span class="keyword">if</span> (rowspan &lt;= <span class="number">1</span> &amp;&amp; colspan &lt;= <span class="number">1</span>)</span><br><span class="line">					<span class="keyword">return</span>;</span><br><span class="line">				<span class="comment">// 跨格开插</span></span><br><span class="line">				$(<span class="string">&quot;tr&quot;</span>, $t).each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">					<span class="keyword">var</span> idx = $(<span class="string">&quot;tr&quot;</span>, $t).index(<span class="built_in">this</span>);</span><br><span class="line">					<span class="keyword">var</span> arr, $td = $(<span class="string">&quot;&lt;td&gt;&quot;</span>).addClass(isSel ? sigSel : sigDel);</span><br><span class="line"></span><br><span class="line">					<span class="keyword">if</span> (idx == ridx) &#123;</span><br><span class="line">						<span class="comment">// 本行在 [cidx] 后插入 colspan-1 个</span></span><br><span class="line"></span><br><span class="line">						arr = $(); <span class="comment">// 准备待插单元格</span></span><br><span class="line">						<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; colspan - <span class="number">1</span>; i++)</span><br><span class="line">							arr = arr.add($td.clone());</span><br><span class="line">						<span class="comment">// 插入</span></span><br><span class="line">						$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).after(arr);</span><br><span class="line"></span><br><span class="line">					&#125; <span class="keyword">else</span> <span class="keyword">if</span> (ridx &lt; idx &amp;&amp; idx &lt; ridx + rowspan) &#123;</span><br><span class="line">						<span class="comment">// 以下行在 [cidx] 前插入 colspan 个</span></span><br><span class="line"></span><br><span class="line">						arr = $(); <span class="comment">// 准备待插单元格</span></span><br><span class="line">						<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; colspan; i++)</span><br><span class="line">							arr = arr.add($td.clone());</span><br><span class="line">						<span class="comment">// 插入</span></span><br><span class="line">						<span class="keyword">if</span> (cidx &gt; <span class="number">0</span> &amp;&amp; $(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx - <span class="number">1</span>).length &gt; <span class="number">0</span>)</span><br><span class="line">							$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx - <span class="number">1</span>).after(arr);</span><br><span class="line">						<span class="keyword">else</span> <span class="keyword">if</span> ($(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).length &gt; <span class="number">0</span>)</span><br><span class="line">							$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).before(arr);</span><br><span class="line">						<span class="keyword">else</span></span><br><span class="line">							$(<span class="built_in">this</span>).prepend(arr);</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;);</span><br><span class="line">			&#125;);</span><br><span class="line"></span><br><span class="line">			<span class="keyword">var</span> rmin = <span class="number">10000</span>,</span><br><span class="line">				cmin = <span class="number">10000</span>;</span><br><span class="line">			<span class="keyword">var</span> rmax = <span class="number">0</span>,</span><br><span class="line">				cmax = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">var</span> rnum, cnum;</span><br><span class="line">			<span class="comment">// 计算起始和跨距</span></span><br><span class="line">			$(<span class="string">&quot;th,td&quot;</span>, $t).filter(<span class="string">&quot;.&quot;</span> + sigSel).each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">				<span class="keyword">var</span> ridx = $(<span class="string">&quot;tr&quot;</span>, $t).index($(<span class="built_in">this</span>).parent(<span class="string">&quot;tr&quot;</span>));</span><br><span class="line">				rmin = ridx &lt; rmin ? ridx : rmin;</span><br><span class="line">				rmax = ridx &gt; rmax ? ridx : rmax;</span><br><span class="line">				<span class="keyword">var</span> cidx = $(<span class="built_in">this</span>).parent().children(<span class="string">&quot;th,td&quot;</span>).index(<span class="built_in">this</span>);</span><br><span class="line">				cmin = cidx &lt; cmin ? cidx : cmin;</span><br><span class="line">				cmax = cidx &gt; cmax ? cidx : cmax;</span><br><span class="line">			&#125;);</span><br><span class="line">			rnum = rmax - rmin + <span class="number">1</span>;</span><br><span class="line">			cnum = cmax - cmin + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">			<span class="comment">// 合并单元格</span></span><br><span class="line">			$(<span class="string">&quot;th,td&quot;</span>, $t).each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">				<span class="keyword">var</span> ridx = $(<span class="string">&quot;tr&quot;</span>, $t).index($(<span class="built_in">this</span>).parent(<span class="string">&quot;tr&quot;</span>));</span><br><span class="line">				<span class="keyword">var</span> cidx = $(<span class="built_in">this</span>).parent().children(<span class="string">&quot;th,td&quot;</span>).index(<span class="built_in">this</span>);</span><br><span class="line">				<span class="comment">// 标记单元格待删</span></span><br><span class="line">				<span class="keyword">if</span> (rmin &lt;= ridx &amp;&amp; ridx &lt;= rmax &amp;&amp;</span><br><span class="line">					cmin &lt;= cidx &amp;&amp; cidx &lt;= cmax)</span><br><span class="line">					$(<span class="built_in">this</span>).addClass(sigDel);</span><br><span class="line">				<span class="comment">// 处理被选左上角单元格</span></span><br><span class="line">				<span class="keyword">if</span> (ridx == rmin &amp;&amp; cidx == cmin)</span><br><span class="line">					$(<span class="built_in">this</span>).removeClass(sigDel).attr(&#123;</span><br><span class="line">						rowspan: rnum,</span><br><span class="line">						colspan: cnum</span><br><span class="line">					&#125;);</span><br><span class="line">				<span class="comment">// 清理残余</span></span><br><span class="line">				<span class="keyword">if</span> ($(<span class="built_in">this</span>).attr(<span class="string">&quot;rowspan&quot;</span>) == <span class="number">1</span>) $(<span class="built_in">this</span>).removeAttr(<span class="string">&quot;rowspan&quot;</span>);</span><br><span class="line">				<span class="keyword">if</span> ($(<span class="built_in">this</span>).attr(<span class="string">&quot;colspan&quot;</span>) == <span class="number">1</span>) $(<span class="built_in">this</span>).removeAttr(<span class="string">&quot;colspan&quot;</span>);</span><br><span class="line">			&#125;).remove(<span class="string">&quot;.&quot;</span> + sigDel);</span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		mergeTable: function () &#123;</span></span><br><span class="line"><span class="comment">			var nodes = this.table.getElementsByClassName(&#x27;ui-selected&#x27;)</span></span><br><span class="line"><span class="comment">			var newNode = document.createElement(&#x27;td&#x27;)</span></span><br><span class="line"><span class="comment">			var parent = nodes[0].parentNode</span></span><br><span class="line"><span class="comment">			newNode.rowSpan = this.table.rowspan</span></span><br><span class="line"><span class="comment">			newNode.colSpan = this.table.colspan</span></span><br><span class="line"><span class="comment">			parent.replaceChild(newNode, nodes[0])</span></span><br><span class="line"><span class="comment">			while (nodes[0]) &#123;</span></span><br><span class="line"><span class="comment">				nodes[0].remove()</span></span><br><span class="line"><span class="comment">			&#125;</span></span><br><span class="line"><span class="comment">		&#125;,*/</span></span><br><span class="line">		mergeDown: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">var</span> row = <span class="built_in">document</span>.getSelection().anchorNode.parentNode.rowIndex</span><br><span class="line">			<span class="keyword">var</span> cell = <span class="built_in">document</span>.getSelection().anchorNode.cellIndex</span><br><span class="line">			<span class="keyword">var</span> node = <span class="built_in">document</span>.getSelection().anchorNode</span><br><span class="line">			<span class="keyword">if</span> (row != <span class="built_in">this</span>.table.rows.length - <span class="number">1</span> &amp;&amp; node.rowSpan == <span class="number">1</span>) &#123;</span><br><span class="line">				node.rowSpan = <span class="number">2</span></span><br><span class="line">				<span class="built_in">this</span>.table.rows[row + <span class="number">1</span>].cells[cell].remove()</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;,</span><br><span class="line">		mergeRight: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">var</span> cell = <span class="built_in">document</span>.getSelection().anchorNode.cellIndex</span><br><span class="line">			<span class="keyword">var</span> node = <span class="built_in">document</span>.getSelection().anchorNode</span><br><span class="line">			<span class="keyword">if</span> (cell != <span class="number">0</span> &amp;&amp; node.colSpan == <span class="number">1</span>) &#123;</span><br><span class="line">				node.previousElementSibling.remove()</span><br><span class="line">				node.colSpan = <span class="number">2</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;,</span><br><span class="line">		splitTable: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">var</span> $t = $(<span class="string">&quot;#&quot;</span> + <span class="built_in">this</span>.tableId);</span><br><span class="line"></span><br><span class="line">			<span class="keyword">if</span> ($(<span class="string">&quot;table&quot;</span>, $t).length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">				alert(<span class="string">&quot;不支持嵌套表格！&quot;</span>);</span><br><span class="line">				<span class="keyword">return</span>;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			<span class="keyword">var</span> sigDel = <span class="string">&quot;sign4delete&quot;</span>; <span class="comment">// 删除标记，类名，自定义</span></span><br><span class="line">			<span class="keyword">var</span> sigSel = <span class="string">&quot;ui-selected&quot;</span>; <span class="comment">// 选中标记，类名，jQuery UI 定义</span></span><br><span class="line"></span><br><span class="line">			<span class="comment">// 补充单元格以便后继正确计算坐标</span></span><br><span class="line">			$(<span class="string">&quot;th,td&quot;</span>, $t).each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">				<span class="comment">// 坐标要实时计算，因会实时补充</span></span><br><span class="line">				<span class="keyword">var</span> ridx = $(<span class="string">&quot;tr&quot;</span>, $t).index($(<span class="built_in">this</span>).parent(<span class="string">&quot;tr&quot;</span>));</span><br><span class="line">				<span class="keyword">var</span> cidx = $(<span class="built_in">this</span>).parent().children(<span class="string">&quot;th,td&quot;</span>).index(<span class="built_in">this</span>);</span><br><span class="line">				<span class="keyword">var</span> rowspan = <span class="built_in">Number</span>($(<span class="built_in">this</span>).attr(<span class="string">&quot;rowspan&quot;</span>)) || <span class="number">1</span>;</span><br><span class="line">				<span class="keyword">var</span> colspan = <span class="built_in">Number</span>($(<span class="built_in">this</span>).attr(<span class="string">&quot;colspan&quot;</span>)) || <span class="number">1</span>;</span><br><span class="line">				<span class="keyword">var</span> isSel = $(<span class="built_in">this</span>).hasClass(sigSel);</span><br><span class="line">				<span class="comment">// 非选单元格拆出的单元格要加删除标记</span></span><br><span class="line"></span><br><span class="line">				<span class="keyword">if</span> (rowspan &lt;= <span class="number">1</span> &amp;&amp; colspan &lt;= <span class="number">1</span>)</span><br><span class="line">					<span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">				<span class="keyword">if</span> (isSel)</span><br><span class="line">					$(<span class="built_in">this</span>).removeAttr(<span class="string">&quot;colspan&quot;</span>).removeAttr(<span class="string">&quot;rowspan&quot;</span>);</span><br><span class="line"></span><br><span class="line">				<span class="comment">// 跨格开插</span></span><br><span class="line">				$(<span class="string">&quot;tr&quot;</span>, $t).each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">					<span class="keyword">var</span> idx = $(<span class="string">&quot;tr&quot;</span>, $t).index(<span class="built_in">this</span>);</span><br><span class="line">					<span class="keyword">var</span> arr, $td = $(<span class="string">&quot;&lt;td&gt;&quot;</span>);</span><br><span class="line"></span><br><span class="line">					<span class="keyword">if</span> (!isSel)</span><br><span class="line">						$td.addClass(sigDel);</span><br><span class="line"></span><br><span class="line">					<span class="keyword">if</span> (idx == ridx) &#123;</span><br><span class="line">						<span class="comment">// 本行在 [cidx] 后插入 colspan-1 个</span></span><br><span class="line"></span><br><span class="line">						arr = $(); <span class="comment">// 准备待插单元格</span></span><br><span class="line">						<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; colspan - <span class="number">1</span>; i++)</span><br><span class="line">							arr = arr.add($td.clone());</span><br><span class="line"></span><br><span class="line">						$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).after(arr);</span><br><span class="line"></span><br><span class="line">					&#125; <span class="keyword">else</span> <span class="keyword">if</span> (ridx &lt; idx &amp;&amp; idx &lt; ridx + rowspan) &#123;</span><br><span class="line">						<span class="comment">// 以下行在 [cidx] 前插入 colspan 个</span></span><br><span class="line"></span><br><span class="line">						arr = $(); <span class="comment">// 准备待插单元格</span></span><br><span class="line">						<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; colspan; i++)</span><br><span class="line">							arr = arr.add($td.clone());</span><br><span class="line"></span><br><span class="line">						<span class="keyword">if</span> (cidx &gt; <span class="number">0</span> &amp;&amp; $(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx - <span class="number">1</span>).length &gt; <span class="number">0</span>)</span><br><span class="line">							$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx - <span class="number">1</span>).after(arr);</span><br><span class="line">						<span class="keyword">else</span> <span class="keyword">if</span> ($(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).length &gt; <span class="number">0</span>)</span><br><span class="line">							$(<span class="string">&quot;th,td&quot;</span>, <span class="built_in">this</span>).eq(cidx).before(arr);</span><br><span class="line">						<span class="keyword">else</span></span><br><span class="line">							$(<span class="built_in">this</span>).prepend(arr);</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;);</span><br><span class="line">			&#125;);</span><br><span class="line"></span><br><span class="line">			<span class="comment">// 重新获取以取到删者并删之</span></span><br><span class="line">			$(<span class="string">&quot;th,td&quot;</span>, $t).remove(<span class="string">&quot;.&quot;</span> + sigDel);</span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		splitTable: function () &#123;</span></span><br><span class="line"><span class="comment">			var node = document.getSelection().anchorNode</span></span><br><span class="line"><span class="comment">			var td = document.createElement(&#x27;td&#x27;)</span></span><br><span class="line"><span class="comment">			if (node.rowSpan == 2 &amp;&amp; node.colSpan == 1) &#123;</span></span><br><span class="line"><span class="comment">				node.rowSpan = 1</span></span><br><span class="line"><span class="comment">				node.parentNode.nextElementSibling.appendChild(td)</span></span><br><span class="line"><span class="comment">			&#125; else if (node.colSpan == 2 &amp;&amp; node.rowSpan == 1) &#123;</span></span><br><span class="line"><span class="comment">				node.colSpan = 1</span></span><br><span class="line"><span class="comment">				node.parentNode.appendChild(td)</span></span><br><span class="line"><span class="comment">			&#125;</span></span><br><span class="line"><span class="comment">		&#125;,*/</span></span><br><span class="line">		addEvent: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="built_in">document</span>.body.onmouseup = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line">		&#125;,</span><br><span class="line">		_init: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">var</span> tableId = <span class="built_in">arguments</span>[<span class="number">0</span>][<span class="number">0</span>];</span><br><span class="line">			<span class="keyword">if</span> (!tableId)</span><br><span class="line">				<span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;table id cannot be null&quot;</span>);</span><br><span class="line">			<span class="keyword">var</span> table = <span class="built_in">document</span>.getElementById(tableId);</span><br><span class="line">			<span class="keyword">if</span> (!table || table.nodeName.toUpperCase() != <span class="string">&#x27;TABLE&#x27;</span>) &#123;</span><br><span class="line">				<span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;canot find the table,please enter the right id&quot;</span>);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">// 侧边和底部的新增按钮</span></span><br><span class="line">			<span class="keyword">var</span> addCellBtn = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;addCell&#x27;</span>)</span><br><span class="line">			<span class="keyword">var</span> addRowBtn = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;addRow&#x27;</span>)</span><br><span class="line">			<span class="built_in">this</span>.tableId = tableId;</span><br><span class="line">			<span class="built_in">this</span>.table = table;</span><br><span class="line"></span><br><span class="line">			<span class="comment">//使表格可选</span></span><br><span class="line">			<span class="built_in">this</span>.table.onmousedown = <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">				<span class="comment">// 鼠标按下的时候获取所在单元格的坐标，和鼠标按下的时间</span></span><br><span class="line">				<span class="built_in">this</span>.startRow = e.target.parentNode.rowIndex</span><br><span class="line">				<span class="built_in">this</span>.startCell = e.target.cellIndex</span><br><span class="line">				<span class="built_in">this</span>.startDate = <span class="keyword">new</span> <span class="built_in">Date</span>()</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="built_in">this</span>.table.onmouseup = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">				<span class="comment">// 通过和鼠标抬起事件所获取的时间进行对比 判断是否是长按鼠标事件</span></span><br><span class="line">				<span class="keyword">var</span> isClick = <span class="keyword">new</span> <span class="built_in">Date</span>() - <span class="built_in">this</span>.startDate &lt; <span class="number">500</span> ? <span class="literal">true</span> : <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">				<span class="comment">// 获取鼠标抬起时 鼠标所在单元格的坐标点</span></span><br><span class="line">				<span class="keyword">var</span> endCell = event.target.cellIndex</span><br><span class="line">				<span class="keyword">var</span> endRow = event.target.parentNode.rowIndex</span><br><span class="line"></span><br><span class="line">				<span class="keyword">if</span> (!isClick) &#123;</span><br><span class="line">					<span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="built_in">Math</span>.min(endRow, <span class="built_in">this</span>.startRow); i &lt;= <span class="built_in">Math</span>.max(endRow, <span class="built_in">this</span>.startRow); i++) &#123;</span><br><span class="line">						<span class="keyword">for</span> (<span class="keyword">let</span> j = <span class="built_in">Math</span>.min(endCell, <span class="built_in">this</span>.startCell); j &lt;= <span class="built_in">Math</span>.max(endCell, <span class="built_in">this</span>.startCell); j++) &#123;</span><br><span class="line">							<span class="comment">// 以获取对角坐标 填充选中的区域</span></span><br><span class="line">							<span class="comment">//this.rows[i].cells[j].className = &#x27;ui-selected&#x27;</span></span><br><span class="line"></span><br><span class="line">							<span class="keyword">if</span> (<span class="built_in">this</span>.rows[i].cells[j]) &#123;</span><br><span class="line">								<span class="keyword">if</span> (<span class="built_in">this</span>.rows[i].cells[j].rowSpan &gt; <span class="number">1</span>) &#123;</span><br><span class="line">									<span class="keyword">if</span> (j == <span class="built_in">Math</span>.max(endCell, <span class="built_in">this</span>.startCell)) &#123;</span><br><span class="line">										<span class="keyword">for</span> (<span class="keyword">let</span> a = <span class="number">1</span>; a &lt; <span class="built_in">this</span>.rows[i].cells[j].rowSpan; a++) &#123;</span><br><span class="line">											<span class="keyword">if</span>(<span class="built_in">this</span>.rows[i + a].cells[j])&#123;</span><br><span class="line">												<span class="built_in">this</span>.rows[i + a].cells[j].className = <span class="string">&#x27;del&#x27;</span></span><br><span class="line">											&#125;</span><br><span class="line">										&#125;</span><br><span class="line">									&#125;<span class="keyword">else</span> <span class="keyword">if</span>((j + table.rows[i].cells[j].colSpan - <span class="number">1</span> )== <span class="built_in">Math</span>.max(endCell, <span class="built_in">this</span>.startCell))&#123;</span><br><span class="line">										<span class="keyword">for</span> (<span class="keyword">let</span> a = <span class="number">1</span>; a &lt; <span class="built_in">this</span>.rows[i].cells[j].rowSpan; a++) &#123;</span><br><span class="line">											<span class="keyword">for</span> (<span class="keyword">let</span> b = <span class="number">0</span>; b &lt; <span class="built_in">this</span>.rows[i].cells[j].colSpan; b++) &#123;</span><br><span class="line">												<span class="keyword">if</span> (<span class="built_in">this</span>.rows[i + a].cells[j+b]) &#123;</span><br><span class="line">													<span class="built_in">this</span>.rows[i + a].cells[j+b].className = <span class="string">&#x27;del&#x27;</span></span><br><span class="line">												&#125;</span><br><span class="line">											&#125;</span><br><span class="line">										&#125;</span><br><span class="line">									&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">										 <span class="keyword">var</span> maxJ = <span class="built_in">Math</span>.max(endCell, <span class="built_in">this</span>.startCell)</span><br><span class="line">										<span class="keyword">for</span> (<span class="keyword">let</span> a = <span class="number">0</span>; a &lt; <span class="built_in">this</span>.rows[i].cells[j].rowSpan; a++) &#123;</span><br><span class="line">											<span class="keyword">if</span> (table.rows[i + a].cells[maxJ]) &#123;</span><br><span class="line">												<span class="built_in">this</span>.rows[i + a].cells[maxJ].className = <span class="string">&#x27;del&#x27;</span></span><br><span class="line">											&#125;</span><br><span class="line">										&#125; </span><br><span class="line"></span><br><span class="line">									&#125;</span><br><span class="line">								&#125;</span><br><span class="line"></span><br><span class="line">								<span class="keyword">if</span> (<span class="built_in">this</span>.rows[i].cells[j].colSpan &gt; <span class="number">1</span>) &#123;</span><br><span class="line">									<span class="keyword">if</span> (j !== endCell &amp;&amp; j !== <span class="built_in">this</span>.startCell) &#123;</span><br><span class="line">										<span class="keyword">for</span> (<span class="keyword">let</span> b = <span class="number">1</span>; b &lt; <span class="built_in">this</span>.rows[i].cells[j].colSpan; b++) &#123;</span><br><span class="line">											<span class="keyword">if</span>(<span class="built_in">this</span>.rows[i].cells[j + b])&#123;</span><br><span class="line">												<span class="built_in">this</span>.rows[i].cells[j + b].className = <span class="string">&#x27;del&#x27;</span></span><br><span class="line">											&#125;			</span><br><span class="line">										&#125;</span><br><span class="line">									&#125;<span class="keyword">else</span> <span class="keyword">if</span>((j + table.rows[i].cells[j].colSpan - <span class="number">1</span> )== <span class="built_in">Math</span>.max(endCell, <span class="built_in">this</span>.startCell))&#123;</span><br><span class="line">										<span class="keyword">for</span> (<span class="keyword">let</span> a = <span class="number">0</span>; a &lt; <span class="built_in">this</span>.rows[i].cells[j].rowSpan; a++) &#123;</span><br><span class="line">											<span class="keyword">for</span> (<span class="keyword">let</span> b = <span class="number">1</span>; b &lt; <span class="built_in">this</span>.rows[i].cells[j].colSpan; b++) &#123;</span><br><span class="line">												<span class="keyword">if</span> (<span class="built_in">this</span>.rows[i + a].cells[j+b]) &#123;</span><br><span class="line">													<span class="built_in">this</span>.rows[i + a].cells[j+b].className = <span class="string">&#x27;del&#x27;</span></span><br><span class="line">												&#125;</span><br><span class="line">											&#125;</span><br><span class="line">										&#125;	</span><br><span class="line">									&#125; </span><br><span class="line">									<span class="keyword">else</span> &#123;</span><br><span class="line">										<span class="built_in">this</span>.rows[i].cells[j].className = <span class="string">&#x27;&#x27;</span></span><br><span class="line">									&#125;</span><br><span class="line">								&#125;</span><br><span class="line">								<span class="built_in">this</span>.rows[i].cells[j].className += <span class="string">&#x27;ui-selected&#x27;</span></span><br><span class="line">							&#125;</span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="comment">// 在table中增加两个属性 获取到跨的行和列 供合并单元格</span></span><br><span class="line">					<span class="built_in">this</span>.rowspan = <span class="built_in">Math</span>.abs(endRow - <span class="built_in">this</span>.startRow) + <span class="number">1</span></span><br><span class="line">					<span class="built_in">this</span>.colspan = <span class="built_in">Math</span>.abs(endCell - <span class="built_in">this</span>.startCell) + <span class="number">1</span></span><br><span class="line"></span><br><span class="line">				&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">					<span class="comment">// 清空样式</span></span><br><span class="line">					<span class="built_in">this</span>.rowspan = endRow</span><br><span class="line">					<span class="built_in">this</span>.colspan = endCell</span><br><span class="line">					<span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; table.rows.length; i++) &#123;</span><br><span class="line">						<span class="keyword">for</span> (<span class="keyword">let</span> j = <span class="number">0</span>; j &lt; table.rows[i].cells.length; j++) &#123;</span><br><span class="line">							<span class="built_in">this</span>.rows[i].cells[j].className = <span class="string">&#x27;&#x27;</span></span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			<span class="built_in">this</span>.table.onmouseenter = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">				addCellBtn.style.display = <span class="string">&#x27;inline-block&#x27;</span></span><br><span class="line">				addRowBtn.style.display = <span class="string">&#x27;block&#x27;</span></span><br><span class="line">			&#125;</span><br><span class="line">			<span class="built_in">this</span>.table.onmouseleave = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">				<span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">					addCellBtn.style.display = <span class="string">&#x27;none&#x27;</span></span><br><span class="line">					addRowBtn.style.display = <span class="string">&#x27;none&#x27;</span></span><br><span class="line">				&#125;, <span class="number">3000</span>);</span><br><span class="line"></span><br><span class="line">			&#125;</span><br><span class="line">			<span class="built_in">this</span>.table.onmousemove = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">				<span class="keyword">if</span> (event.clientX - <span class="built_in">this</span>.offsetLeft &gt;= <span class="number">8</span> &amp;&amp; event.clientX - <span class="built_in">this</span>.offsetLeft &lt;= <span class="number">20</span>) &#123;</span><br><span class="line">					<span class="built_in">this</span>.style.cursor = <span class="string">&quot;url(&#x27;img/right.png&#x27;) , auto&quot;</span></span><br><span class="line">					<span class="built_in">this</span>.colspan = <span class="built_in">this</span>.rows.length</span><br><span class="line">				&#125; <span class="keyword">else</span> <span class="keyword">if</span> (event.clientY - <span class="built_in">this</span>.offsetTop &gt;= <span class="number">52</span> &amp;&amp; event.clientY - <span class="built_in">this</span>.offsetTop &lt;= <span class="number">60</span>) &#123;</span><br><span class="line">					<span class="built_in">this</span>.style.cursor = <span class="string">&quot;url(&#x27;img/down.png&#x27;) , auto&quot;</span></span><br><span class="line">				&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">					<span class="built_in">this</span>.style.cursor = <span class="string">&#x27;auto&#x27;</span></span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="built_in">this</span>.table.onclick = <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">				<span class="keyword">if</span> (<span class="built_in">this</span>.style.cursor !== <span class="string">&#x27;auto&#x27;</span>) &#123;</span><br><span class="line">					<span class="keyword">if</span> (<span class="built_in">this</span>.style.cursor.includes(<span class="string">&#x27;down&#x27;</span>)) &#123;</span><br><span class="line">						<span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="built_in">this</span>.rows.length; i++) &#123;</span><br><span class="line">							<span class="built_in">this</span>.rows[i].cells[e.target.cellIndex].className = <span class="string">&#x27;ui-selected&#x27;</span></span><br><span class="line">						&#125;</span><br><span class="line">						<span class="built_in">this</span>.rowspan = <span class="built_in">this</span>.rows.length</span><br><span class="line">					&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">						<span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="built_in">this</span>.rows[e.target.parentNode.rowIndex].cells.length; i++) &#123;</span><br><span class="line">							<span class="built_in">this</span>.rows[e.target.parentNode.rowIndex].cells[i].className = <span class="string">&#x27;ui-selected&#x27;</span></span><br><span class="line">						&#125;</span><br><span class="line">						<span class="built_in">this</span>.colspan = <span class="built_in">this</span>.rows[e.target.parentNode.rowIndex].cells.length</span><br><span class="line">						<span class="built_in">this</span>.rowspan = <span class="number">1</span></span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">return</span> <span class="built_in">this</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	kclass.prototype = kclass.fn;</span><br><span class="line">	win_.ytable = (win_.ytable || kclass);</span><br><span class="line">&#125;)(<span class="built_in">window</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/02/27/table/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/04/09/ES2020%E6%96%B0%E7%89%B9%E6%80%A7/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            ES2020新特性
          
        </div>
      </a>
    
    
      <a href="/2020/02/03/%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0%E5%AF%8C%E6%96%87%E6%9C%AC/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">JavaScript实现简单文本编辑器</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "9OLyDF6ymKbs1woL9kh2qbae-gzGzoHsz",
    app_key: "RTkmORaJHqkGjEovHixLK04x",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2019-2023
        <i class="ri-heart-fill heart_icon"></i> John Doe
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.webp" alt="Doreen&#39;s Blog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->


<script src="/js/clickBoom2.js"></script>


<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>